from datetime import datetime
from openai import OpenAI

def get_completion_glm(prompt: str, model: str, temperature: float, api_key: str, max_tokens: int):
    """获取GLM回答"""
    try:
        # 使用自定义 API 端点
        URL = "http://aliyun01.server.pmnlplab.top:61585/v1"
        client = OpenAI(api_key='EMPTY', base_url=URL)
        
        response = client.chat.completions.create(
            model=model,
            messages=[
                {"role": "user", "content": "你是谁？"},
                {"role": "assistant", "content": "我是武汉大学RAG知识库助手，我可以帮助你回答问题。"},
                {"role": "user", "content": "你有什么功能？"},
                {"role": "assistant", "content": "我可以帮助你回答问题，你可以问我任何问题。你可以上传文件，我可以通过阅读文件来回答问题。"},
                {"role": "user", "content": "今天是几号？"},
                {"role": "assistant", "content": "今天的日期是：" + datetime.now().strftime("%Y-%m-%d")},  # 用函数给出实时时刻
                {"role": "user", "content": prompt}
            ],
            temperature=temperature,
            max_tokens=max_tokens
        )
        
        return response.choices[0].message.content
        
    except Exception as e:
        return f"调用失败：{str(e)}"
